package com.atguigu.teach.juc;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;


class MyThread implements Callable<String>
{
    @Override
    public String call() throws Exception
    {
        System.out.println(Thread.currentThread().getName()+"\t"+"---come in");
        //暂停4秒钟线程
        try { TimeUnit.SECONDS.sleep(6); }
        catch (InterruptedException e) { e.printStackTrace(); }
        return "hello 210419java";
    }
}


/**
 * @auther zzyy
 * @create 2020-03-28 16:30
 * java里面，第三种获得多线程的方式
 * 一个新的接口，Callable
 */
public class CallableDemo
{
    public static void main(String[] args) throws ExecutionException, InterruptedException
    {
        FutureTask<String> futureTask = new FutureTask<>(new MyThread());
        new Thread(futureTask,"A").start();

        FutureTask<String> futureTask2 = new FutureTask<>(new MyThread());
        new Thread(futureTask2,"B").start();


        System.out.println(Thread.currentThread().getName()+"\t"+"---do biz ......result1");
        System.out.println(futureTask.get()+"\t"+"result2");//最好将futureTask的get方法，放在最后调用




    }
}